const Logic = require('../logic');
const Validater = require('fastest-validator');
module.exports = class extends Logic
{

    constructor(ctx)
    {
        super(ctx);
        this.V = new Validater();
    }

    async postAction()
    {
        let data = think.stringToJson(this.post('data'));
        if (data === false)
        {
            return this.fail(52004, '提交的表单信息错误,该数据必须为json格式,请检查数据格式,上传数据格式为{\'data\',yourJsonData},如 : $.post("http://127.0.0.1:8091/apiv1/competition",{\'data\':yourJsonData}),yourJsonData为你要上传的数据包');
        }
        const cpguid = data.cpguid;
        this.ctx.state.cpguid = cpguid;
        if (think.isNullOrUndefined(cpguid))
        {
            return this.fail(52022, 'cpguid不存在,请检查cpguid');
        }

        let applyType = await this.model('apiv1/competition').where({PK_CompetitionGuid: cpguid}).getField('ApplyType', 1);


        if (applyType[0] === '团队')
        {
            //todo 验证团队数据
            let teamRule = {
                userguid: {type: "string", min: 1},
                TeamName: {type: "string", min: 1},
                Menmber: {type: "array",},
                OrgName: {type: "string", min: 1},
                Name: {type: "string", min: 1},
                Tel: {type: "string", min: 5, max: 11},
                Email: {type: "email",},
            };
            let vres = think.validate(data.team, teamRule);
            if (vres === true)
            {
                this.ctx.state.teamData = data.team;
            } else
            {
                return this.error(52030, vres[0].message);
            }
        } else if (applyType[0] === '个人')
        {
            let psrRule = {
                userguid: {type: "string", min: 1},
                Name: {type: "string", min: 1},
                OrgName: {type: "string", min: 1},
                Tel: {type: "string", min: 1, max: 11},
                Email: {type: "email"},
            };
            let vres = think.validate(data.psr, psrRule);
            if (vres === true)
            {
                this.ctx.state.psrData = data.psr;

            } else
            {
                return this.error(52030, vres[0].message);
            }

        } else
        {
            return this.fail(52023, 'cpguid或比赛不存在,请检查cpguid');
        }

    }

};
